<?php namespace Summer\Box\ReportWidgets;

use Backend\Classes\ReportWidgetBase;
use BackendAuth;
use Exception;
use Illuminate\Support\Facades\DB;
use Flash;
use Input;
use Summer\Hjd\Classes\BonusApi;
use Validator;
use ValidationException;
use ApplicationException;

class Fafang extends ReportWidgetBase
{
    public function render()
    {
        try {
            $this->loadData();
        }
        catch (Exception $ex) {
            $this->vars['error'] = $ex->getMessage();
        }
        return $this->makePartial('widget');
    }

    public function defineProperties()
    {
        return [
            'title' => [
                'title'             => '批量发放扣除',
                'default'           => '批量发放扣除',
                'type'              => 'string',
                'validationPattern' => '^.+$',
                'validationMessage' => '批量发放扣除错误'
            ]
        ];
    }

    public function onFafang()
    {
        $rules = [
            'type' => 'required',
            'initpwd' => 'required',
        ];
        $messages = [
            'type.required'    => '请选择发放币种.',
            'initpwd.required'    => '密码不能为空.',
        ];
        $validation = Validator::make(post(), $rules,$messages);
        if ($validation->fails()) {
            throw new ValidationException($validation);
        }
        try {
            $initpwd=Input::get('initpwd');
            $user = BackendAuth::getUser();
            if (!$user->checkHashValue('password', $initpwd)) {
                throw new ApplicationException("密码错误");
            }
            $type=Input::get('type');
            $bonus_api=new BonusApi();
            $bonus_api->fafangRed($type);
            Flash::success('批量发放扣除成功!');
        }catch (Exception $ex) {
            Flash::error($ex->getMessage());
        }

    }
    protected function loadData()
    {
    }
}
